package org.grew.openEntity.tools;

import org.grew.openEntity.po.system.ClassInfo;

import javax.persistence.Column;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Administrator
 * @Date: 2019/1/8 11:03
 * @Description:
 */
public class AnyTools {

    /**
     * 根据名称获取class内的field的中文说明以及数据库类型
     * @param name
     * @return
     * @throws ClassNotFoundException
     */
    public static List<ClassInfo> parseClass(String name) throws ClassNotFoundException{
        if(!name.isEmpty()){
            name = name.substring(0, 1).toUpperCase().concat(name.substring(1,name.length()));
        }
        System.out.println(name);
        List<ClassInfo> list = new ArrayList<ClassInfo>();
        String obj = "org.grew.openEntity.po.logic."+name.concat("Obj");
        Class<?> clazz =  Class.forName(obj);
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            Column annotation = field.getAnnotation(Column.class);
            if(annotation==null)continue;
            String columnDefinition = annotation.columnDefinition();
            String comment = columnDefinition.substring(columnDefinition.indexOf("\'"), columnDefinition.lastIndexOf("\'"));
            String tableType = columnDefinition.substring(0, columnDefinition.indexOf(" "));
            System.out.println(comment+"+++++++"+tableType);
            list.add(new ClassInfo(comment,tableType));
        }
        return list;
    }

}
